<?php
/**
 * Created by fengkl
 * User: Administrator
 * Date: 2018年5月21日 18:03:56
 * 
 */

namespace app\common\logic;

use app\common\model\Store;
use app\common\model\Product;
use app\common\validate;
use app\index\controller\Common;
use think\Db;

class ProductLogic extends Common
{
    /**
     * 门店列表
     */
    public function getList($map,$order,$size)
    {
        $pdmodel = new Product();
        $field = 'a.*, b.ca_name, b.p_id';
        $info = $pdmodel->getList($map,$order,$size,$field);
        foreach ($info as $k => $v) {
           $pic_info = explode(',', $v['pd_pic']);
           if(!$pic_info[0]){
                array_shift($pic_info);
            }
            $info[$k]['pd_pic'] = $pic_info;
        }
        return $info;
    }
    public function getListNpg($map,$order,$size)
    {
        $pdmodel = new Product();
        $field = 'a.*, b.ca_name, b.p_id';
        $info = $pdmodel->getListNpg($map,$order,$size,$field);
        foreach ($info as $k => $v) {
           $pic_info = explode(',', $v['pd_pic']);
           if(!$pic_info[0]){
                array_shift($pic_info);
            }
            $info[$k]['pd_pic'] = $pic_info;
        } 
        return $info;
    }

    /**
     * 商品信息
     */
    public function getOne($id)
    {
        $pdmodel = new Product();
        $map['a.id'] = $id;
        $field = 'a.*,b.ca_name';
        $info = $pdmodel->getOne($map,$field);        
        $pics = explode(",", $info['pd_pic']);
        if(!$pics[0]){
            array_shift($pics);
        }
        $info['pd_pic'] = $pics;
        return $info;
    }


    /**
     * 商品添加和修改
     */
    public function saveProduct($data,$code = '')
    {

        $validate = validate('Verify');
        $rel = array();
        $srcs = array_column($data['pd_pic'], 'url');
        if($srcs){
            $data['pd_pic'] = implode(",", $srcs);
        }else{
            $aaa = $data['pd_pic'];
            $data['pd_pic'] = implode(",", $aaa);
        }      
        $data['pd_pic'] = str_replace("\\", "/", $data['pd_pic']);
        //var_dump($data['pd_pic']);
        $rst = $validate->scene('addProduct')->check($data);
        if (!$rst) {
            $rel['code'] = 0;
            $rel['msg'] = $validate->getError();
            return $rel;
        }
        if ($data['pd_price']<0) {
            $this->e_msg('价格不能为负数');
        }

        $data['pd_add_time'] = date('Y-m-d H:i:s');
        //最终价格
//        if ($data['ca_id'] == 8) {
//            $pd_total = ($data['pd_price']) - ($data['pd_now_price']);
//            if ($pd_total < 0) {
//                return $rel = ['code' => 0, 'msg' => '折扣价格不能大于商品价格'];
//            }
//
//            $data['pd_total'] = $pd_total;
//        }
//        $num = [9, 10, 11, 12];
//        if (in_array($data['ca_id'], $num)) {
//            $pd_total = $data['pd_price'];
//            $data['pd_now_price'] = 0;
//            $data['pd_total'] = $pd_total;
//        }




        //$data['pd_now_price'] = $data['pd_now_price'];
//        if ($data['ca_id'] == 10) {
//            $data['pd_now_price'] = $data['pd_price'] * 0.9;
//        }else{
//            $data['pd_now_price'] = 0;
//        }
        // $cate_pid = model('cate')->where('id',$data['ca_id'])->value('p_id');
        // if(!$cate_pid){
        //     $rel['code'] = 0;
        //     $rel['msg'] = '请不要选择商品大分类！';
        //     return $rel;
        // }   
        if($code == 1){
            //修改操作            
            $map['id'] = $data['pd_id'];
            unset($data['pd_id']);

//            halt($data);

            $rell = db('product')->where($map)->update($data);
            if($rell){
                $rel['code'] = 1;
                $rel['msg'] = '修改成功！';
            }else{
                $rel['code'] = 0;
                $rel['msg'] = '您没有作出修改！';
            }
            return $rel;
        }
        
        // 编号
        /*$number = model("Store")->order('id desc')->value('st_num');
        if ($number) {
            $bb = substr($number, -5);
            $cc = substr($number, 0, 2);
            $dd = $bb + 1;
            $new_number = $cc . $dd;
        } else {
            $new_number = 'md10001';
        }
        $data['st_num'] = $new_number;*/
        //添加操作 
//        var_dump($data);exit();
        $rell = model('Product')->addInfo($data);
        if($rell){
            $rel['code'] = 1;
            $rel['msg'] = '添加成功！';
        }else{
            $rel['code'] = 0;
            $rel['msg'] = '添加失败！';
        }
        return $rel;
    }

    /**
     * 分类添加
     * 
     */
    public function saveCate($data)
    {
        $validate = validate('Verify');
        $rel = array();
        $rst = $validate->scene('addCate')->check($data);
        if (!$rst) {
            $rel['code'] = 0;
            $rel['msg'] = $validate->getError();
            return $rel;
        }
        $data['ca_add_time'] = date('Y-m-d H:i:s');
        //添加操作      
        $rell = model('Cate')->addInfo($data);
        if($rell){
            $rel['code'] = 1;
            $rel['msg'] = '添加成功！';
        }else{
            $rel['code'] = 0;
            $rel['msg'] = '添加失败！';
        }
        return $rel;

    }
    /**
     * 商品规格添加
     */
    public function saveSku($data){
        $validate = validate('Verify');
        $rel = array();
        $rst = $validate->scene('addSku')->check($data);
        if (!$rst) {
            $rel['code'] = 0;
            $rel['msg'] = $validate->getError();
            return $rel;
        }
        $rell = model('skus')->addInfo($data);
        if($rell){
            $rel['code'] = 1;
            $rel['msg'] = '添加成功！';
        }else{
            $rel['code'] = 0;
            $rel['msg'] = '添加失败！';
        }
        return $rel;
    }

    /**
     * 判断传递的浮点型，并且小数点后，不能超过两位数
     * @param $num 经过验证器判断的附浮点数
     */
    public function num($num){
        if ($num < 0) {
            $this->e_msg('价格不能为负数');
        }

    }

}